package com.example.lenovo.appstore.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.example.lenovo.appstore.R;
import com.example.lenovo.appstore.activity.RankListActivity;
import com.example.lenovo.appstore.adapter.HomeAppListAdapter;
import com.example.lenovo.appstore.utils.AppContext;
import com.example.lenovo.appstore.utils.StatusBarUtil;
import com.example.lenovo.appstore.view.TitleBarView;
import com.example.lenovo.model.CategoryListModel;
import com.youth.banner.Banner;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：AppStore
 *
 * @Author 郭迎辉
 * Create by 9:36 on 2019/3/20.
 * Describe:
 */
public class CategoryFragment extends Fragment implements View.OnClickListener {
    @Bind(R.id.search)
    SearchView searchView;
    @Bind(R.id.list)
    RecyclerView list;
    @Bind(R.id.rank1)
    TextView rank1;
    @Bind(R.id.rank2)
    TextView rank2;
    @Bind(R.id.rank3)
    TextView rank3;
    @Bind(R.id.rank4)
    TextView rank4;
    HomeAppListAdapter adapter;
    private View view;
    public static CategoryFragment instance;

    public static CategoryFragment getInstance() {
        if (null == instance) {
            instance = new CategoryFragment();
        }
        return instance;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 隐藏软键盘
        getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        StatusBarUtil.setStatusBarLightMode(getActivity().getWindow());
        ArrayList<CategoryListModel.CategoryList> datas = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            datas.add(new CategoryListModel.CategoryList());
        }
        adapter = new HomeAppListAdapter(getActivity(), datas);
        list.setLayoutManager(new LinearLayoutManager(getActivity()));
        list.setAdapter(adapter);
        list.setHasFixedSize(true);
        list.setNestedScrollingEnabled(false);
        adapter.notifyDataSetChanged();
        rank1.setOnClickListener(this);
        rank2.setOnClickListener(this);
        rank3.setOnClickListener(this);
        rank4.setOnClickListener(this);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_category, container, false);
            ButterKnife.bind(this, view);
        } else {
            if (null != view.getParent()) {
                ((ViewGroup) view.getParent()).removeAllViews();
            }

        }
        instance = this;
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rank1:
                Intent intent1 = new Intent(getActivity(), RankListActivity.class);
                intent1.putExtra("code", 1);
                startActivity(intent1);
                break;
            case R.id.rank2:
                Intent intent2 = new Intent(getActivity(), RankListActivity.class);
                intent2.putExtra("code", 2);
                startActivity(intent2);
                break;
            case R.id.rank3:
                Intent intent3 = new Intent(getActivity(), RankListActivity.class);
                intent3.putExtra("code", 3);
                startActivity(intent3);
                break;
            case R.id.rank4:
                Intent intent4 = new Intent(getActivity(), RankListActivity.class);
                intent4.putExtra("code", 4);
                startActivity(intent4);
                break;

        }
    }
}
